package com.yh.csx.crm.service;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.yh.csx.crm.core.exception.BusinessException;
import com.yh.csx.crm.core.util.DateUtils;
import com.yh.csx.crm.dao.CustomerMapper;
import com.yh.csx.crm.dao.SysProvinceMapper;
import com.yh.csx.crm.po.Customer;
import com.yh.csx.crm.po.SysProvince;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: zeng
 * @create: 2019-03-15 15:34
 **/
@Service
public class AppTempService {
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    SysProvinceMapper sysProvinceMapper;




       /*
       * @Author zeng
       * @Date -
       **/
    public void judgeTempLimit(Long customerId, BigDecimal tempCredit , Date endDate) {
        if (!this.sevenDayNoRepeat(customerId)){
            throw new BusinessException("有生效临时额度有效期内不能重复申请");
        }

        if (null == customerId) {
            throw new BusinessException("没有接收到客户ID");
        }
        //查询客户信息
        Customer customer = customerMapper.selectByPrimaryKey(customerId);
        if (null == customer) {
            throw new BusinessException("没有该客户");
        }
        if (null == customer.getRegionProvince()) {
            throw new BusinessException("该客户没有省份信息");
        }
        //拿到客户省份编码
        String provinceCode = customer.getSalesProvince();

        tempCredit=tempCredit.subtract(customer.getCreditLimit());
        this.prTemp(provinceCode,tempCredit,endDate);
    }

    public boolean sevenDayNoRepeat(Long customerId){
        Customer customer = customerMapper.selectByPrimaryKey(customerId);
        if (!(new BigDecimal(0.00).compareTo(customer.getTempCreditLimit())==0)){
            Date date = DateUtils.oneDayEndTime(customer.getTempEndTime());
            Date d =  DateUtils.oneDayEndTime(new Date());
            if (d.getTime() <= date.getTime()){
                return false;
            }
        }
        return true;
    }

    public void  prTemp(String provinceCode ,BigDecimal tempCredit, Date endDate){
        if (!this.isLatestWeek(endDate)){
            throw new BusinessException("临时额度只能申请今日起七天以后有效时间");
        }
        //根据省份编码查询省份
        SysProvince sysProvince = sysProvinceMapper.selectByProvinceCode(provinceCode);
        if (null == sysProvince) {
            throw new BusinessException("客户省份信息异常");
        }
        //获得该省临时额度
        BigDecimal limit = sysProvince.getLimit();
        //查询该省临时额度使用了多少
        BigDecimal prAllLimit = customerMapper.selectProvinceAllLimit(provinceCode);
        if (null == prAllLimit){
            prAllLimit = new BigDecimal(0);
        }
        if (limit.compareTo(prAllLimit.add(tempCredit)) < 0) {
            //当省区额度不足时
            throw new BusinessException("客户省区临时额度不足");
        }
    }

    public boolean isLatestWeek(Date endDate){
        Date date = DateUtils.oneDayEndTime(endDate);
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(DateUtils.oneDayEndTime(new Date()));//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, +7);  //设置为7天后
        Date before7days = calendar.getTime();   //得到7天后的时间
        if(before7days.getTime() >= date.getTime()){
            return true;
        }else{
            return false;
        }
    }
}
